# a note on the project routes:
class ProjectsController < ApplicationController
  # in the :only block, list the actions that need to have a @project
  before_filter :get_project, :only => [ :show, :edit, :update, :delete ]
  skip_before_filter :verify_authenticity_token, :only =>
      [ :autocomplete, :autocomplete_status, :autocomplete_priority ]
  before_filter :authorize_admin, :only => [ :new, :create, :destroy ]
  before_filter :authorize, :only => [ :edit, :update ]

  # List all projects
  #
  # GET /projects
  # GET /projects.xml
  # GET /projects.json
  def index
    @projects = Project.find(:all)
    @has_feed = true

    respond_to do |format|
      format.html # index.html.erb
      format.rss  # index.rss.builder
      format.xml  { render :xml => @projects }
      format.json { render :json => @projects.to_json }
    end
  end

  # Show a project based on name.
  #
  # GET /projects/foo
  # GET /projects/foo.xml
  # GET /projects/foo.json
  def show
    # gets @project from :get_project filter
    @latest = @project.issues.open
    @recent = @project.issues.recent

    respond_to do |format|
      if @project.nil? then
        format.html { render :action => "new" }
        format.xml  { render :xml => "no project", :status => :unprocessable_entity }
        format.json { render :json => "no project".to_json, :status => :unprocessable_entity }
      else
        format.html # show.html.erb
        format.xml  { render :xml => @project }
        format.json { render :json => @project.to_json }
      end
    end
  end

  # GET /projects/new
  # GET /projects/new.xml
  def new
    @project = Project.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @project }
    end
  end

  # GET /projects/1/edit
  def edit
    # gets @project from :get_project filter
  end

  # POST /projects
  # POST /projects.xml
  def create
    @project = Project.new(params[:project])
    @project.members << @current_user  # assign the current user to this project

    respond_to do |format|
      if @project.save
        flash[:notice] = 'Project was successfully created.'
        format.html { redirect_to(@project) }
        format.xml  { render :xml => @project, :status => :created, :location => @project }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /projects/1
  # PUT /projects/1.xml
  def update
    # gets @project from :get_project filter

    respond_to do |format|
      if @project.update_attributes(params[:project])
        flash[:notice] = 'Project was successfully updated.'
        format.html { redirect_to( :project_name => @project.name ) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /projects/1
  # DELETE /projects/1.xml
  def destroy
    # gets @project from :get_project filter

    @project.destroy

    respond_to do |format|
      format.html { redirect_to(projects_url) }
      format.xml  { head :ok }
    end
  end

  # GET /projects/autocomplete
  def autocomplete
    # :after => true means names must begin with +match+
    @names = Project.names( params[:match], :after => true )
    render :action => 'autocomplete', :layout => false
  end

  # NOTE: these next two are here (in the project controller) in case we want
  # to have project-specific status or priorities in the future.  it would be
  # easy to implement, just store a string, split it and send the values.

  # GET /projects/project1/issues/autocomplete_status
  def autocomplete_status
    # add the match so the user can type + hit enter
    @names = [params[:match]] + Status.names
    # make sure there is at least one 'closed' status
    @names << 'closed' unless @names.include? 'closed'
    render :action => 'autocomplete', :layout => false
  end

  # GET /projects/project1/issues/autocomplete_priority
  def autocomplete_priority
    # add the match so the user can type + hit enter
    @names = [params[:match]] + Priority.names
    render :action => 'autocomplete', :layout => false
  end

private

  def get_project
    @project = Project.find_by_name_or_id( params[:id] )
  end

end
